package com.example.hu.UI;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.csht.facelib.db.DbManager;
import com.csht.facelib.db.User;
import com.csht.facelib.manager.CshtCompareUtil;
import com.csht.facelib.utils.FileUtil;
import com.csht.facelib.utils.ToastUtils;
import com.csht.facelib.utils.Tools;
import com.example.hu.Adapter.MyRlUserAdapter;
import com.example.hu.Appliction.MyApp;
import com.example.hu.R;

import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


public class UserEditActivity extends BaseActivity {

    private ImageView iv_pic;
    private EditText et_name;
    private RecyclerView rl;
    private String filePath;
    private ExecutorService es = Executors.newSingleThreadExecutor();
    private Future future;
    private MyRlUserAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_edit);

        init();

        adapter = new MyRlUserAdapter();
        adapter.bindToRecyclerView(rl);
        adapter.setNewData(DbManager.getInstance().queryAllUser());

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter ada, View view, int position) {
                User user = adapter.getData().get(position);
                DbManager.getInstance().deleteUser(user);
                adapter.remove(position);
            }
        });
    }

    private void init() {
        et_name = findViewById(R.id.et_name);
        iv_pic = findViewById(R.id.iv_pic);
        rl = findViewById(R.id.rl);
        rl.setLayoutManager(new LinearLayoutManager(this));

        findViewById(R.id.btn_camera).setOnClickListener(this);
        findViewById(R.id.btn_save).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_camera:
                startActivityForResult(new Intent(this,CameraActivity.class),1001);
                break;
            case R.id.btn_save:
                final String name = et_name.getText().toString();
                if (TextUtils.isEmpty(name)){
                    ToastUtils.showToast("请输入姓名");
                } else if (TextUtils.isEmpty(filePath)){
                ToastUtils.showToast("请选择照片");
                } else {
                    Tools.hideKeyBoard(null,this);
                    if (future != null && !future.isDone()) {
                        ToastUtils.showToast("正在保存，请稍后再试");
                        return;
                    }
                    future = es.submit(new Runnable() {
                        @Override
                        public void run() {
                            User user = new User();
                            user.setName(name);
                            user.setIdPhoto(filePath);
                            user.setFeature(CshtCompareUtil.getInstance().getFeatureFromData(FileUtil.file2Bytes(new File(filePath)),true));
                            DbManager.getInstance().insertUser(user);
                            ToastUtils.showToast("保存成功");
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    et_name.setText("");
                                    filePath = null;
                                    iv_pic.setImageResource(R.mipmap.ic_launcher);
                                    adapter.setNewData(DbManager.getInstance().queryAllUser());
                                    CshtCompareUtil.getInstance().initDatabases();

                                }
                            });
                        }
                    });
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (resultCode == RESULT_OK && data != null){
                if (requestCode == 1001){
                    filePath = data.getStringExtra("filePath");
                    iv_pic.setImageBitmap(BitmapFactory.decodeFile(filePath));
                }
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
